package com.sun.stringescapeutils;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class MapVsFlatMap {
    public static void main(String[] args) {
        Flux<String> mapFlux = Flux.just(1, 2, 3)
                .map(i -> "id #" + i);
        subscribeFlux("mapFlux", mapFlux);

        Flux<String> flatMapFlux = Flux.just(1, 2, 3)
                .flatMap(i -> Mono.just("id #" + i));
        subscribeFlux("flatMapFlux", flatMapFlux);
    }

    private static void subscribeFlux(String varName, Flux<?> flux) {
        flux.doOnSubscribe(s -> System.out.print(varName + ":"))//在流之前做的事情
                .doOnNext(e -> System.out.print(e + ", "))//下一个流的处理
                .doOnComplete(System.out::println)//流的最后换行
                .subscribe();//没有这个，流中不会有东西
    }
}
